// 资源js
var $scope = null;
$(function() {
	var appElement = document.querySelector('[ng-controller=main]');
	$scope = angular.element(appElement).scope();
	  initialPage($scope);
	  initPaginator(1);
});
// 初始化值
function initialPage($scope) {
	// 初始化
	var dynamicTypeId = locationHref("dynamicTypeId");
	if(dynamicTypeId){
		$scope.search.dynamicTypeId=dynamicTypeId
		$scope.selectedlabel=dynamicTypeId
		 $scope.$apply();
		  $scope.methods.dynamicList(dynamicTypeId,1);
	}else{
		$scope.search.dynamicTypeId=1
		$scope.selectedlabel=1
		 $scope.$apply();
		  $scope.methods.dynamicList(1,1);
	}
	
	
	
}
function initPaginator(page){
	var optionsInit = {
	    currentPage: 1,//当前的请求页数。
	    totalPages: 1,//一共多少页。
	    size:"normal",//应该是页眉的大小。
	    bootstrapMajorVersion: 3,//bootstrap的版本要求。
	    alignment:"right",
	    numberOfPages:5,//一页列出多少数据。
	};
	$("#pageLimit").bootstrapPaginator(optionsInit);
}
var mainApp = angular.module("mainApp", []);
mainApp.controller('main', function($scope,$sce) {
	$scope.search={};
	$scope.dynamicTypeList=[{
		"dynamicTypeName":'图集',"dynamicTypeId":'1'},{
		"dynamicTypeName":'影音',"dynamicTypeId":'2'},{
		"dynamicTypeName":'三维模型',"dynamicTypeId":'3'}
		]
	$scope.methods = {
		// 点击后列表
		labelHeadClick: function (label) {
		    $scope.search.dynamicTypeId = label.dynamicTypeId;
			$scope.selectedlabel = label.dynamicTypeId;
		    $scope.methods.dynamicList(label.dynamicTypeId,1);
		},
		// 图集
		dynamicList : function(dynamicTypeId,page) {
			var realUrl;
			var params = '';
			if(dynamicTypeId==1){//图集
				realUrl='/website/imageTypeList?'
				params += '&status=1'
				params += '&isPublic=Y'
			}else if(dynamicTypeId==2){//影音
				realUrl='/website/videoList?'
			}else{//三维模型
				realUrl='/website/modelList?'
				params += '&status=1'
			}
			
			params += '&pageNum='+page
			params += '&pageSize=12'
			$.ajax({
			    url:$.shysUrlPrefix+realUrl+params,
			    type:'get',
			    dataType:'JSON',
				async:true,  //同步方式发起请求
				beforeSend: function () {
				      $(".loading").show();
				  },
			    success:function (data) {
					if (data.rows != null && data.rows.length > 0 ) {
					 $.each(data.rows,function(i,item){
						 if(data.rows[i].imageTypeCover){
							 data.rows[i].imageTypeCover=data.rows[i].imageTypeCover;
							// data.rows[i].imageTypeCover=data.rows[i].imageTypeCover+'?x-oss-process=image/watermark,color_FFFFFF,text_QOahpeaigeWcqOe6vw' 
						 }	
						if(data.rows[i].modelCover){
							data.rows[i].modelCover=data.rows[i].modelCover+'?x-oss-process=image/watermark,color_FFFFFF,text_QOahpeaigeWcqOe6vw' 
						}					 
					 }),
					   $scope.dynamicList = data.rows;
					    $scope.$apply();
					   var totalPages= Math.ceil(data.total/12);
					   if(totalPages <2){
					       totalPages = 1;
					   }
					   var options = {
					       currentPage: page,//当前的请求页数。
					       totalPages: totalPages,//一共多少页。
					       size:"normal",//应该是页眉的大小。
					       bootstrapMajorVersion: 5,//bootstrap的版本要求。
					       alignment:"right",
					       numberOfPages:5,//显示按钮的数量。
					       itemTexts: function (type, page, current) {//如下的代码是将页眉显示的中文显示我们自定义的中文。
					           switch (type) {
					           case "first": return "首页";
					           case "prev": return "上一页";
					           case "next": return "下一页";
					           case "last": return "尾页";
					           case "page": return page;
					           }
					       },
					       onPageClicked: function (event, originalEvent, type, page){//给每个页眉绑定一个事件，其实就是ajax请求，其中page变量为当前点击的页上的数字。
					   		$scope.methods.dynamicList(dynamicTypeId,page);
					   		
					   		
					   
					       }
					   };
					   $("#pageLimit").bootstrapPaginator(options);
					} else {
					   
					}
					
			    },
				complete: function () {
				       $(".loading").hide();
				   },
			
			})
		},
		// 跳转详情页
		labelDetailClick: function (label) {
			console.log($scope.search.dynamicTypeId)
		 console.log(label)
		 if($scope.search.dynamicTypeId==1&&label.imageTypeId){
			  window.location.href="resource-sDetail.html?imageTypeId="+label.imageTypeId
		 }else if($scope.search.dynamicTypeId==2&&label.videoId){
			  window.location.href="video-detail.html?videoId="+label.videoId
		 }else if($scope.search.dynamicTypeId==3&&label.modelId){
			  // window.location.href="model-detail.html?modelId="+label.modelId
			  $.ajax({
			  	url: $.shysUrlPrefix + '/website/model/' +label.modelId,
			  	type: 'get',
			  	dataType: 'JSON',
			  	async: true, //同步方式发起请求
			  	success: function(data) {
			  		if (data.data != null ) {
			  			window.open(data.data.modelUrl);
			  			$scope.$apply();
			  		} else {
			  		
			  		}
			  		
			  	},
			  		
			  })
		 }
		
		},
		
		
	},
	$scope.videoUrlFun = function(url){
	    var urlFun = $sce.trustAsResourceUrl(url);
	    return urlFun;
	};
});











